package com.jic.veteran.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.jic.veteran.common.exception.ApiException;
import com.jic.veteran.mapper.*;
import com.jic.veteran.model.*;
import com.jic.veteran.portal.service.AlumniSchoolService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 获取学校相关接口
 * Created by mars on 2021/11/10.
 */
@Slf4j
@Service
public class AlumniSchoolServiceImpl implements AlumniSchoolService {

    @Autowired
    private AlumniSchoolMapper alumniSchoolMapper;
    @Autowired
    private AlumniSchoolNewsMapper alumniSchoolNewsMapper;
    @Autowired
    private AlumniSchoolAccommodationMapper alumniSchoolAccommodationMapper;
    @Autowired
    private AlumniSchoolTeachersMapper alumniSchoolTeachersMapper;


    /**
     * 获取学校介绍
     * Created by mars on 2021/11/10.
     */
    @Override
    public AlumniSchool getSchoolExplain() {
        AlumniSchoolExample alumniSchoolExample = new AlumniSchoolExample();
        AlumniSchoolExample.Criteria criteria = alumniSchoolExample.createCriteria();
        criteria.andStatusEqualTo(1);
        AlumniSchool alumniSchool = alumniSchoolMapper.selectByExampleWithBLOBs(alumniSchoolExample).get(0);
        if (alumniSchool.getId() == null || alumniSchool.getContent() == null) {
            throw new ApiException("未找到详情信息");
        }
        return alumniSchool;
    }

    /**
     * 获取学校全部新闻
     * Created by mars on 2021/11/10.
     */
    @Override
    public List<AlumniSchoolNews> getSchoolAllNews() {

        List<AlumniSchoolNews> alumniSchoolNews ;

        AlumniSchoolNewsExample alumniSchoolNewsExample = new AlumniSchoolNewsExample();
        AlumniSchoolNewsExample.Criteria criteria = alumniSchoolNewsExample.createCriteria();
        criteria.andStatusEqualTo(1);
        alumniSchoolNewsExample.setOrderByClause("create_time desc");
        alumniSchoolNews = alumniSchoolNewsMapper.selectByExampleWithBLOBs(alumniSchoolNewsExample);
        return alumniSchoolNews;
    }

    /**
     * 获取学校分页新闻
     * Created by mars on 2021/11/10.
     */
    @Override
    public List<AlumniSchoolNews> getSchoolPageNews(Integer pageSize,Integer pageNum) {

        PageHelper.startPage(pageNum, pageSize);
        List<AlumniSchoolNews> alumniSchoolNews ;

        AlumniSchoolNewsExample alumniSchoolNewsExample = new AlumniSchoolNewsExample();
        AlumniSchoolNewsExample.Criteria criteria = alumniSchoolNewsExample.createCriteria();
        criteria.andStatusEqualTo(1);
        alumniSchoolNewsExample.setOrderByClause("create_time desc");
        alumniSchoolNews = alumniSchoolNewsMapper.selectByExampleWithBLOBs(alumniSchoolNewsExample);
        return alumniSchoolNews;
    }

    /**
     * 获取学校单个新闻详情
     * Created by mars on 2021/11/10.
     */
    @Override
    public AlumniSchoolNews getSchoolNews(Long id) {
        AlumniSchoolNewsExample alumniSchoolNewsExample = new AlumniSchoolNewsExample();
        AlumniSchoolNewsExample.Criteria criteria = alumniSchoolNewsExample.createCriteria();
        criteria.andStatusEqualTo(1)
                .andIdEqualTo(id);
        AlumniSchoolNews alumniSchoolNews = alumniSchoolNewsMapper.selectByExampleWithBLOBs(alumniSchoolNewsExample).get(0);
        if (alumniSchoolNews.getId() == null || alumniSchoolNews.getContent() == null) {
            throw new ApiException("未找到详情信息");
        }
        return alumniSchoolNews;
    }

    /**
     * 获取学校全部周边食宿
     * Created by mars on 2021/11/10.
     */
    @Override
    public List<AlumniSchoolAccommodation> getAllAccommodation() {


        List<AlumniSchoolAccommodation> alumniSchoolAccommodation ;

        AlumniSchoolAccommodationExample alumniSchoolAccommodationExample = new AlumniSchoolAccommodationExample();
        AlumniSchoolAccommodationExample.Criteria criteria = alumniSchoolAccommodationExample.createCriteria();
        criteria.andStatusEqualTo(1);
        alumniSchoolAccommodationExample.setOrderByClause("create_time desc");
        alumniSchoolAccommodation = alumniSchoolAccommodationMapper.selectByExampleWithBLOBs(alumniSchoolAccommodationExample);
        return alumniSchoolAccommodation;
    }

    /**
     * 获取学校全部老师
     * Created by mars on 2021/11/10.
     */
    @Override
    public List<AlumniSchoolTeachers> getTeachers() {
        List<AlumniSchoolTeachers> alumniSchoolTeachers ;

        AlumniSchoolTeachersExample alumniSchoolTeachersExample = new AlumniSchoolTeachersExample();
        AlumniSchoolTeachersExample.Criteria criteria = alumniSchoolTeachersExample.createCriteria();
        criteria.andStatusEqualTo(1);
        alumniSchoolTeachersExample.setOrderByClause("create_time desc");
        alumniSchoolTeachers = alumniSchoolTeachersMapper.selectByExampleWithBLOBs(alumniSchoolTeachersExample);
        return alumniSchoolTeachers;
    }

    /**
     * 获取学校分页老师
     * Created by mars on 2021/11/10.
     */
    @Override
    public List<AlumniSchoolTeachers> getPageTeachers(Integer pageSize,Integer pageNum) {

        PageHelper.startPage(pageNum, pageSize);
        List<AlumniSchoolTeachers> alumniSchoolTeachers ;

        AlumniSchoolTeachersExample alumniSchoolTeachersExample = new AlumniSchoolTeachersExample();
        AlumniSchoolTeachersExample.Criteria criteria = alumniSchoolTeachersExample.createCriteria();
        criteria.andStatusEqualTo(1);
        alumniSchoolTeachersExample.setOrderByClause("create_time desc");
        alumniSchoolTeachers = alumniSchoolTeachersMapper.selectByExampleWithBLOBs(alumniSchoolTeachersExample);
        return alumniSchoolTeachers;
    }


    /**
     * 获取学校单个老师详情
     * Created by mars on 2021/11/10.
     */
    @Override
    public AlumniSchoolTeachers getTeacher(Long id) {
        AlumniSchoolTeachersExample alumniSchoolTeachersExample = new AlumniSchoolTeachersExample();
        AlumniSchoolTeachersExample.Criteria criteria = alumniSchoolTeachersExample.createCriteria();
        criteria.andStatusEqualTo(1)
                .andIdEqualTo(id);
        AlumniSchoolTeachers alumniSchoolTeachers = alumniSchoolTeachersMapper.selectByExampleWithBLOBs(alumniSchoolTeachersExample).get(0);
        return alumniSchoolTeachers;
    }

}
